<?php
/**
 * Created by PhpStorm.
 * User: ljg
 * Date: 2020/7/2
 * Time: 19:37
 */
/**
 * @param $type
 * @return string
 * @context 奖品类型
 */
function getsPrizeCategory($type)
{
    switch ($type){
        case 1:
            return "积分";
            break;
        case 2:
            return "金币";
            break;
        case 3:
            return "卡券";
            break;
        case 4:
            return "恢复券";
            break;
        case 5:
            return "其他奖品";
            break;
        default:
            return "";
    }
}

/**
 * @param $type
 * @return string
 * @context 获取任务类型
 */
function getsTaskType($type)
{
    switch ($type){
        case 1:
            return "推荐玩家";
            break;
        case 2:
            return "达成级别";
            break;
        case 3:
            return "转轮摇奖";
            break;
        case 4:
            return "金币转账";
            break;
        case 5:
            return "团队人数";
            break;
        case 6:
            return "发红包";
            break;
        case 7:
            return "连续签到";
            break;
        case 8:
            return "生意出售";
            break;
        case 9:
            return "生意购买";
            break;
        case 10:
            return "兑换材料";
            break;
        case 11:
            return "积分转账";
            break;
        case 12:
            return "生意交租";
            break;
        case 13:
            return "激活账号";
            break;
        case 14:
            return "首次注册";
            break;
        case 15:
            return "打劫玩家";
            break;
        case 16:
            return "反击玩家";
            break;
        case 17:
            return "每日答题";
            break;
        case 18:
            return "卡券使用";
            break;
        default:
            return "";
    }
}

/**
 * @param $str
 * @return mixed
 * @context 去除富文本中图片地址的upload
 */
function wipeOffFormStr($str)
{
    return str_replace('/upload/','',$str);
}

/**
 * @param $stemp
 * @return false|string
 * @context 时间戳转为日期时间
 */
function getsTimeFormat($stemp)
{
    return date("Y-m-d H:i:s",$stemp);
}

function diffSuspend($start_time,$end_time)
{
    # 计算时间内经历多少暂停时间
    # 只判断当前时间距离开始时间有多少暂停、结束时间距离开始时间有多少暂停
    $basic=date("Y-m-d H:i:s")<$end_time ? date("Y-m-d H:i:s") : $end_time;
    # 计算与开始时间相差多少天
    $diff=intval(diffBetweenTwoDays($start_time,$basic));
    # 判断结束日期是否大于当天的22点
    if(date("Y-m-d 22:00:00",strtotime($end_time))<=$basic){
        $diff+=1;
    }
    $addTime=$diff*11;
    return date("Y-m-d H:i:s",strtotime("+{$addTime} hours",strtotime($end_time)));
}

/**
 * @param $day1
 * @param $day2
 * @return float|int
 * @context 计算两个日期相差多少天
 */
function diffBetweenTwoDays ($day1, $day2)
{
    $second1 = strtotime($day1);
    $second2 = strtotime($day2);

    if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
    }
    return ($second1 - $second2) / 86400;
}


function businessMeetCondition($arr)
{
    if($arr['status']!=1){
        return false;
    }else{
        return true;
    }
}

/**
 * @return bool
 * @context 设置活跃玩家数量
 */
function setActiveMember(){
    $redis=new \Redis\Redis();
    $_all=$redis->hGet("all_member","all_member");
    if(!empty($_all)){
        $_multiple=rand(1,10);
        $_all+=$_multiple;
        $redis->hSet("all_member","all_member",strval($_all));
    }
    return true;
}

/**
 * 格式化数字
 */
function float_number($number){
    $length = strlen($number);  //数字长度
    if($length > 8){ //亿单位
        $str = sprintf("%.3f", $number/100000000)."亿";
    }elseif($length >4){ //万单位
        //截取前俩为
        $str = sprintf("%.1f", $number/10000)."万";
    }else{
        return $number;
    }
    return $str;
}

/**
 * @return array
 * @context 等级匹配数组
 */
function matchingLevelCommon()
{
    return [
        [1],
        [1,2],
        [1,2,3],
        [2,3,4],
        [2,3,4,5],
        [3,4,5,6],
        [3,4,5,6,7],
        [4,5,6,7,8],
        [4,5,6,7,8,9],
        [5,6,7,8,9,10],
        [5,6,7,8,9,10,11],
        [6,7,8,9,10,11,12],
    ];
}


/**
 * @param $level
 * @return mixed
 * @context 等级对应的匹配等级
 */
function MatchingLevelPublic($level)
{
    $section=matchingLevelCommon();
    return $section[$level-1];
}

/**
 * @return array
 * @context 可选择价值区间
 */
function canUseValueSectionCommon()
{
    return [
        [[200,500],[500,1000],[1000,2000],[2000,2500]],
        [[200,900],[900,1500],[1500,2200],[2200,3000]],
        [[200,900],[900,1600],[1800,2600],[2600,3600]],
        [[400,1200],[1200,2000],[2000,3000],[3000,4000]],
        [[400,1500],[1500,2500],[2500,3800],[3800,4800]],
        [[600,1800],[1800,3000],[3000,4200],[4200,5500]],
        [[600,2000],[2000,3500],[3500,4800],[4800,6000]],
        [[800,2200],[2200,3800],[3800,5500],[5500,7000]],
        [[800,2500],[2500,4000],[4000,6000],[6000,8500]],
        [[1000,3000],[3000,5000],[5000,7500],[7500,10000]],
        [[1000,4000],[4000,7000],[7000,9000],[9000,12000]],
        [[1200,4500],[4500,8000],[8000,11500],[11500,15000]],
    ];
}

/**
 * @param $level
 * @return mixed
 * @context 获取等级对应可选价值区间
 */
function getCanUseValueSectionCommon($level)
{
    $list=canUseValueSectionCommon();
    return $list[$level-1];
}

/**
 * @param $industry_id
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @context 获取行业详情
 */
function getsIndustryDetail($industry_id)
{
    $_Redis=new \Redis\Redis();
    $list=$_Redis->hGetJson("industryDetail",strval($industry_id));
    if(empty($list)){
        $detail = \think\Db::table('industry i')
            ->field('i.id,i.title,logo,min_worth,max_worth,i.level,i.desc,detail,ml.title levelTitle,i.day_rent,
                (select min(day_income) from industry_cycleincome where industry_id=i.id) min_ratio,
                (select max(day_income) from industry_cycleincome where industry_id=i.id) max_ratio,
                (select min(cycle_period) from industry_cycleincome where industry_id=i.id) min_period,
                (select max(cycle_period) from industry_cycleincome where industry_id=i.id) max_period
                ')
            ->leftJoin('member_level ml','ml.level=i.level')
            ->where(array('i.id' => $industry_id))
            ->find();
        $cycleIncome = \think\Db::table('industry_cycleincome')->where(array('industry_id' => $industry_id))->select();
        $detail['detail'] = \app\service\ResourceService::ContentStaticReplace($detail['detail']);
        $list = array('info' => $detail, 'cycleIncome' => $cycleIncome);
        $_Redis->hSetJson('industryDetail', strval($industry_id), $list);
    }
    return $list;
}

/**
 * @param $code
 * @param array $list
 * @return array|null
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @context 获取所有下级
 */
function getsTeamChild($code,&$list=array())
{
    $result=\think\Db::table("member")->field("code")->where(array("p_code"=>$code))->select();
    if(!empty($result)){
        foreach($result as $k=>$v){
            array_push($list,$v['code']);
            getsTeamChild($v['code'],$list);
        }
    }else{
        return null;
    }
    return $list;
}

/**
 * @param $level
 * @return mixed
 * @context 获取对应等级的金币交易手续费
 */
function getsGoldTradeCharge($level)
{
    $list=goldTradeCharge();
    return $list[$level];
}

/**
 * @return array
 * @context 金币交易手续费
 */
function goldTradeCharge()
{
    return [2,3,5,8,13,0];
}

/**
 * @return array
 * @context 金币出售数量等级
 */
function goldTradeLevel()
{
    return [50,100,200,500,800,1000];
}

/**
 * @return array
 * @context 金币转账选择数量等级
 */
function transferLevel()
{
    return [50,100,200,500,800,1000];
}